﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TVNDBLayer.BLL;

public partial class login : System.Web.UI.Page
{
    AccountBLL acount;
    protected void Page_Load(object sender, EventArgs e)
    {
        acount = new AccountBLL();
        lblError.Text = "";

    }
    public const string MatchEmailPattern =
            @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
     + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
				[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
     + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
				[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
     + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";

    public static bool IsEmail(string email)
    {
        if (email != null) return Regex.IsMatch(email, MatchEmailPattern);
        else return false;
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        string userName = txtUserName.Text;
        string password = txtPassword.Text;

        if (IsEmail(userName))
        {
            int id = acount.GetAccountByEmailAndPassword(userName, password);
            if (id > 0)
            {
                Session["UID"] = id;
                if (Request.QueryString["ref"] != null)
                    Response.Redirect(Request.QueryString["ref"]);
                else
                    Response.Redirect("home.aspx");
            }
            else
            {
                lblError.Text = "Vui lòng kiểm tra thông tin đăng nhập";
                return;
            }
        }
        else
        {
            int id = acount.GetAccountByUserAndPassword(userName, password);
            if (id > 0)
            {
                Session["UID"] = id;
                if (Request.QueryString["ref"] != null)
                    Response.Redirect(Request.QueryString["ref"]);
                else
                    Response.Redirect("home.aspx");
            }
            else
            {
                lblError.Text = "Vui lòng kiểm tra thông tin đăng nhập";
                return;
            }
        }
        Response.Redirect(Request.RawUrl);
    }
}